﻿/******************************************************************************
文件名称    :   MySqlChemistryUrine24hDataProvider.cs
创建日期    :   2011/6/5 7:14:52
功能描述    :   化验24小时实体数据提供程序MySql实现类
修改日志    :   
    2011-06-05 07:14  马利
*******************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using Dare.Utilities.Data;
using Stone.Data.Providers;
using Stone.Components.Entities;

namespace Stone.Data.MySql.Providers
{
    /// <summary>
    /// 化验24小时实体数据提供程序MySql实现类
    /// </summary>
    public class MySqlChemistryUrine24hDataProvider : ChemistryUrine24hDataProvider
    {
        public MySqlChemistryUrine24hDataProvider()
            : base()
        {
        }

        public override void Insert(TransactionManager manager, ChemistryUrine24h entity)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/

                //插入SQL语句
                cmd.CommandText = "insert into tb_chemistry_urine24h(ca, na, ka, cl, acid, sugar, creatinine, uric_acid, citric_acid, phosphorus, ph, comment) values(?ca, ?na, ?ka, ?cl, ?acid, ?sugar, ?creatinine, ?uric_acid, ?citric_acid, ?phosphorus, ?ph, ?comment);" +
                    "select id, time_stamp from tb_chemistry_urine24h where id = last_insert_id();";
                GenericDbHelper.AddParameter(mgr, cmd, "?ca", DbType.Decimal).Value = entity.Ca == null ? DBNull.Value : (object)entity.Ca.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?na", DbType.Decimal).Value = entity.Na == null ? DBNull.Value : (object)entity.Na.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?ka", DbType.Decimal).Value = entity.Ka == null ? DBNull.Value : (object)entity.Ka.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?cl", DbType.Decimal).Value = entity.Cl == null ? DBNull.Value : (object)entity.Cl.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?acid", DbType.Decimal).Value = entity.Acid == null ? DBNull.Value : (object)entity.Acid.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?sugar", DbType.Decimal).Value = entity.Sugar == null ? DBNull.Value : (object)entity.Sugar.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?creatinine", DbType.Decimal).Value = entity.Creatinine == null ? DBNull.Value : (object)entity.Creatinine.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?uric_acid", DbType.Decimal).Value = entity.UricAcid == null ? DBNull.Value : (object)entity.UricAcid.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?citric_acid", DbType.Decimal).Value = entity.CitricAcid == null ? DBNull.Value : (object)entity.CitricAcid.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?phosphorus", DbType.Decimal).Value = entity.Phosphorus == null ? DBNull.Value : (object)entity.Phosphorus.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?ph", DbType.Decimal).Value = entity.Ph == null ? DBNull.Value : (object)entity.Ph.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?comment", DbType.String).Value = entity.Comment == null ? DBNull.Value : (object)entity.Comment;

                //执行数据库操作,并获得主键和更新时间戳
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        entity.Id = reader.GetInt32(0);
                        entity.TimeStamp = reader.GetDateTime(1);
                        entity.EntityState = Stone.Components.EntityState.Saved;
                    }
                }

                //插入关联
                InsertRelations(mgr, cmd, entity);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void Import(TransactionManager manager, IEnumerable<ChemistryUrine24h> entities)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/

                //插入SQL语句
                cmd.CommandText = "insert into tb_chemistry_urine24h(id, ca, na, ka, cl, acid, sugar, creatinine, uric_acid, citric_acid, phosphorus, ph, comment, time_stamp) values(?id, ?ca, ?na, ?ka, ?cl, ?acid, ?sugar, ?creatinine, ?uric_acid, ?citric_acid, ?phosphorus, ?ph, ?comment, ?time_stamp);";
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?ca", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?na", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?ka", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?cl", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?acid", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?sugar", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?creatinine", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?uric_acid", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?citric_acid", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?phosphorus", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?ph", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?comment", DbType.String);
                GenericDbHelper.AddParameter(mgr, cmd, "?time_stamp", DbType.DateTime);

                //执行数据库操作
                foreach (ChemistryUrine24h entity in entities)
                {
                    int i = 0;
                    cmd.Parameters[i++].Value = entity.Id;
                    cmd.Parameters[i++].Value = entity.Ca == null ? DBNull.Value : (object)entity.Ca.Value;
                    cmd.Parameters[i++].Value = entity.Na == null ? DBNull.Value : (object)entity.Na.Value;
                    cmd.Parameters[i++].Value = entity.Ka == null ? DBNull.Value : (object)entity.Ka.Value;
                    cmd.Parameters[i++].Value = entity.Cl == null ? DBNull.Value : (object)entity.Cl.Value;
                    cmd.Parameters[i++].Value = entity.Acid == null ? DBNull.Value : (object)entity.Acid.Value;
                    cmd.Parameters[i++].Value = entity.Sugar == null ? DBNull.Value : (object)entity.Sugar.Value;
                    cmd.Parameters[i++].Value = entity.Creatinine == null ? DBNull.Value : (object)entity.Creatinine.Value;
                    cmd.Parameters[i++].Value = entity.UricAcid == null ? DBNull.Value : (object)entity.UricAcid.Value;
                    cmd.Parameters[i++].Value = entity.CitricAcid == null ? DBNull.Value : (object)entity.CitricAcid.Value;
                    cmd.Parameters[i++].Value = entity.Phosphorus == null ? DBNull.Value : (object)entity.Phosphorus.Value;
                    cmd.Parameters[i++].Value = entity.Ph == null ? DBNull.Value : (object)entity.Ph.Value;
                    cmd.Parameters[i++].Value = entity.Comment == null ? DBNull.Value : (object)entity.Comment;
                    cmd.Parameters[i++].Value = entity.TimeStamp;
                    cmd.ExecuteNonQuery();
                }

                //插入关联
                InsertRelations(mgr, cmd, entities);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void Update(TransactionManager manager, ChemistryUrine24h entity)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //更新SQL语句
                cmd.CommandText = "update tb_chemistry_urine24h set ca = ?ca, na = ?na, ka = ?ka, cl = ?cl, acid = ?acid, sugar = ?sugar, creatinine = ?creatinine, uric_acid = ?uric_acid, citric_acid = ?citric_acid, phosphorus = ?phosphorus, ph = ?ph, comment = ?comment where id = ?id;"
                        + "select time_stamp from tb_chemistry_urine24h where id = ?id;";
                GenericDbHelper.AddParameter(mgr, cmd, "?ca", DbType.Decimal).Value = entity.Ca == null ? DBNull.Value : (object)entity.Ca.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?na", DbType.Decimal).Value = entity.Na == null ? DBNull.Value : (object)entity.Na.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?ka", DbType.Decimal).Value = entity.Ka == null ? DBNull.Value : (object)entity.Ka.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?cl", DbType.Decimal).Value = entity.Cl == null ? DBNull.Value : (object)entity.Cl.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?acid", DbType.Decimal).Value = entity.Acid == null ? DBNull.Value : (object)entity.Acid.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?sugar", DbType.Decimal).Value = entity.Sugar == null ? DBNull.Value : (object)entity.Sugar.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?creatinine", DbType.Decimal).Value = entity.Creatinine == null ? DBNull.Value : (object)entity.Creatinine.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?uric_acid", DbType.Decimal).Value = entity.UricAcid == null ? DBNull.Value : (object)entity.UricAcid.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?citric_acid", DbType.Decimal).Value = entity.CitricAcid == null ? DBNull.Value : (object)entity.CitricAcid.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?phosphorus", DbType.Decimal).Value = entity.Phosphorus == null ? DBNull.Value : (object)entity.Phosphorus.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?ph", DbType.Decimal).Value = entity.Ph == null ? DBNull.Value : (object)entity.Ph.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?comment", DbType.String).Value = entity.Comment == null ? DBNull.Value : (object)entity.Comment;
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64).Value = entity.Id;

                //执行数据库操作,并获得更新时间戳
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        entity.TimeStamp = reader.GetDateTime(0);
                        entity.EntityState = Stone.Components.EntityState.Saved;
                    }
                }

                //更新关联
                UpdateRelations(mgr, cmd, entity);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void Delete(TransactionManager manager, long key)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/

                //先删除关联
                DeleteRelations(mgr, cmd, key);

                //删除SQL语句
                cmd.CommandText = "delete from tb_chemistry_urine24h where id = ?id;";
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64).Value = key;

                //执行
                cmd.ExecuteNonQuery();
                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void DeleteAll(TransactionManager manager, string whereClause)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------具体SQL操作开始----------------------------------*/

                //先删除关联
                DeleteRelations(mgr, cmd, whereClause);

                //删除SQL语句
                cmd.CommandText = String.Format("delete from tb_chemistry_urine24h {0}",
                     ProcessWhereClause(whereClause));

                //执行
                cmd.ExecuteNonQuery();

                /*------------------------------具体SQL操作结束----------------------------------*/
            });
        }

        public override ChemistryUrine24h Get(TransactionManager manager, long key)
        {
            ChemistryUrine24h entity = null;
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = "select * from tb_chemistry_urine24h where id = ?id";
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64).Value = key;

                //执行获取实体
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        entity = Fill(reader, null);
                    }
                }

                //获取关联
                if (entity != null)
                {
                    RetrieveRelations(mgr, cmd, entity);
                    entity.EntityState = Stone.Components.EntityState.Saved;
                }

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return entity;
        }

        public override List<ChemistryUrine24h> GetPaged(TransactionManager manager, string whereClause, string orderBy, int pageIndex, int pageSize)
        {
            List<ChemistryUrine24h> list = new List<ChemistryUrine24h>(pageSize > 0 ? Math.Min(pageSize, 256) : 32);
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = String.Format("select * from tb_chemistry_urine24h {0} {1} {2}",
                    ProcessWhereClause(whereClause),
                    ProcessOrderBy(orderBy),
                    ProcessPaging(pageIndex, pageSize));

                //执行获取实体集
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        list.Add(Fill(reader, null));
                    }
                }

                //获取关联
                RetrieveRelations(mgr, cmd, list);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return list;
        }

        public override int GetCount(TransactionManager manager, string whereClause)
        {
            int count = 0;
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = String.Format("select count(*) from tb_chemistry_urine24h {0}",
                    ProcessWhereClause(whereClause));

                //执行获取实体集数量
                count = Convert.ToInt32(cmd.ExecuteScalar());

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return count;
        }

        public override long GetVersion(TransactionManager manager, string whereClause)
        {
            long count = 0;
            DateTime? timeStamp = null;
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = String.Format("select count(*), max(time_stamp) from tb_chemistry_urine24h {0}",
                    ProcessWhereClause(whereClause));

                //执行获取实体集数量
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        count = reader.GetInt64(0);
                        timeStamp = reader.IsDBNull(1) ? (DateTime?)null : reader.GetDateTime(1);
                    }
                }

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return (timeStamp ?? DateTime.MinValue).Ticks ^ count;
        }

        public override ChemistryUrine24h Fill(IDataReader reader, ChemistryUrine24h entity)
        {
            if (entity == null) entity = new ChemistryUrine24h();

            for (int i = 0; i < reader.FieldCount; i++)
            {
                switch (reader.GetName(i))
                {
                    case "id":
                        entity.Id = reader.GetInt64(i);
                        break;
                    case "ca":
                        entity.Ca = reader[i] == DBNull.Value ? entity.Ca : reader.GetDecimal(i);
                        break;
                    case "na":
                        entity.Na = reader[i] == DBNull.Value ? entity.Na : reader.GetDecimal(i);
                        break;
                    case "ka":
                        entity.Ka = reader[i] == DBNull.Value ? entity.Ka : reader.GetDecimal(i);
                        break;
                    case "cl":
                        entity.Cl = reader[i] == DBNull.Value ? entity.Cl : reader.GetDecimal(i);
                        break;
                    case "acid":
                        entity.Acid = reader[i] == DBNull.Value ? entity.Acid : reader.GetDecimal(i);
                        break;
                    case "sugar":
                        entity.Sugar = reader[i] == DBNull.Value ? entity.Sugar : reader.GetDecimal(i);
                        break;
                    case "creatinine":
                        entity.Creatinine = reader[i] == DBNull.Value ? entity.Creatinine : reader.GetDecimal(i);
                        break;
                    case "uric_acid":
                        entity.UricAcid = reader[i] == DBNull.Value ? entity.UricAcid : reader.GetDecimal(i);
                        break;
                    case "citric_acid":
                        entity.CitricAcid = reader[i] == DBNull.Value ? entity.CitricAcid : reader.GetDecimal(i);
                        break;
                    case "phosphorus":
                        entity.Phosphorus = reader[i] == DBNull.Value ? entity.Phosphorus : reader.GetDecimal(i);
                        break;
                    case "ph":
                        entity.Ph = reader[i] == DBNull.Value ? entity.Ph : reader.GetDecimal(i);
                        break;
                    case "comment":
                        entity.Comment = reader[i] == DBNull.Value ? entity.Comment : reader.GetString(i);
                        break;
                    case "time_stamp":
                        entity.TimeStamp = reader.GetDateTime(i);
                        break;
                }
            }

            entity.EntityState = Stone.Components.EntityState.Saved;
            return entity;
        }

        #region 实体关联操作数据提供类

        #endregion
    }
}
